package com.cjlgb.design.upms.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.cjlgb.design.common.core.bean.ResultBean;
import com.cjlgb.design.common.mybatis.service.impl.BaseServiceImpl;
import com.cjlgb.design.upms.api.channel.AccountOutputChannel;
import com.cjlgb.design.upms.api.entity.Oauth2Account;
import com.cjlgb.design.upms.api.entity.SysUserInfo;
import com.cjlgb.design.upms.channel.SysUserChannel;
import com.cjlgb.design.upms.mapper.SysUserInfoMapper;
import com.cjlgb.design.upms.service.SysUserRoleService;
import com.cjlgb.design.upms.service.SysUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.util.Collection;

/**
 * @author WFT
 * @date 2019/8/11
 * description: 后台用户服务层接口实现
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SysUserServiceImpl extends BaseServiceImpl<SysUserInfoMapper,SysUserInfo> implements SysUserService {

    private final AccountOutputChannel accountOutputChannel;
    private final SysUserChannel userChannel;
    private final SysUserRoleService userRoleService;

    @Override
    public ResultBean createUser(SysUserInfo userInfo) {
        //  校验用户名是否存在
        SysUserInfo dbUserInfo = this.selectByAccountName(userInfo.getAccountName(), null);
        Assert.isNull(dbUserInfo,"登录名称已存在");
        //  填充字段
        userInfo.setAccountId(IdWorker.getId());
        userInfo.setUserPortrait("https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png");
        userInfo.setDeptId(1L);
        userInfo.setDeptName("测试部门");
        userInfo.setLockFlag(0);
        LocalDateTime now = LocalDateTime.now();
        userInfo.setCreateTime(now);
        userInfo.setLastModifyTime(now);
        if (this.save(userInfo)){
            //  异步添加用户与角色间的关联
            this.userChannel.output().send(
                    MessageBuilder.withPayload(userInfo).setHeader("method","add").build()
            );
        }
        return ResultBean.error("保存失败");
    }

    @Override
    public ResultBean updateLockFlag(SysUserInfo userInfo) {
        //  获取账号信息
        SysUserInfo dbUserInfo = this.getById(userInfo.getId());
        Assert.notNull(dbUserInfo,"Id不存在");
        if (this.updateById(userInfo)){
            //  异步修改oauth2账号的锁定状态
            this.accountOutputChannel.output().send(
                    MessageBuilder.withPayload(new Oauth2Account(dbUserInfo.getAccountName(),userInfo.getLockFlag()))
                            .setHeader("method", "isEnable").build()
            );
        }
        return ResultBean.error("保存失败");
    }

    @Override
    public ResultBean updateByAccountId(SysUserInfo userInfo) {
        SysUserInfo dbUserInfo = this.selectInfoByAccountId(userInfo.getAccountId());
        Assert.notNull(dbUserInfo,"账号Id不存在");
        userInfo.setAccountName(dbUserInfo.getAccountName());
        //  异步添加用户与角色间的关联
        this.userChannel.output().send(
                MessageBuilder.withPayload(userInfo).setHeader("method","buildRole").build()
        );
        return ResultBean.ok();
    }

    @Override
    public SysUserInfo selectInfoByAccountId(long accountId) {
        //  根据账号Id查询用户信息
        SysUserInfo userInfo = this.selectByAccountId(accountId);
        Assert.notNull(userInfo,"账号Id不存在");
        //  获取关联的角色Id列表
        userInfo.setRoleIds(this.userRoleService.selectRoleIds(accountId));
        return userInfo;
    }

    @Override
    public void deleteByAccountIds(Collection<Long> accountIds) {
        //  根据账号Id批量删除
        Assert.isTrue(this.removeByAccountIds(accountIds),"账号Id不存在");
        //  通知队列删除用户
        this.userChannel.output().send(
                MessageBuilder.withPayload(accountIds).setHeader("method","delete").build()
        );
    }
}
